package com.bxd.baodan.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.bxd.baodan.App;
import com.bxd.baodan.R;
import com.bxd.baodan.activity.MainActivity;
import com.bxd.baodan.activity.MsgActivity;
import com.bxd.baodan.activity.SearchQuestionActivity;
import com.bxd.baodan.activity.StartAskActivity;
import com.bxd.baodan.adapter.PlanerRecycleAdapter;
import com.bxd.baodan.app.JsonApi;
import com.bxd.baodan.item.PlanerBean.PlanerBean_;
import com.bxd.baodan.item.SortBean;
import com.bxd.baodan.item.SortItem;
import com.bxd.baodan.util.GsonUtil;
import com.bxd.baodan.util.JSONUtil;
import com.bxd.baodan.util.NetUtil;
import com.bxd.baodan.widget.PagerSlidingTabStrip;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

public class AllQuestionFragment extends BaseFragment {
    private ViewPager viewPager;
    private List<BaseFragment> list = new ArrayList<BaseFragment>();
    private FragmentStatePagerAdapter pagerAdapter;
    private PagerSlidingTabStrip tabs;
    private List<SortItem> sortlist = new ArrayList<>();
    private View viewRoot;
    private RecyclerView recyclerView;
    private List<PlanerBean_> planerList = new ArrayList<>();
    private PlanerRecycleAdapter planerAdapter;
    private TextView search;
    public ImageView ring_msg;
    private ImageView start_question;


    public static AllQuestionFragment getInstance() {
        return new AllQuestionFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        viewRoot = inflater.inflate(R.layout.fragment_all_question, container, false);
        initView(viewRoot);

        return viewRoot;
    }


    private void initView(View view) {


        view.setPadding(view.getPaddingLeft(), view.getPaddingTop() + getStatusBarHeight(),
                view.getPaddingRight(), view.getPaddingBottom());

        recyclerView = view.findViewById(R.id.recyclerView);
        search = view.findViewById(R.id.edit_search);
        ring_msg = view.findViewById(R.id.ring_msg);
        start_question = view.findViewById(R.id.start_question);

        start_question.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MobclickAgent.onEvent(App.getContext(), "v6_1_3_BW_wytw");
                openActivity(StartAskActivity.class);
            }
        });

        ring_msg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (loginStatus()) {
                    MobclickAgent.onEvent(App.getContext(), "v6_1_3_BW");
                    openActivity(MsgActivity.class);
                }
            }
        });

        if (((MainActivity) getActivity()).hasUnRead) {
            ring_msg.setImageResource(R.drawable.ring_msg_red);

        } else {
            ring_msg.setImageResource(R.drawable.ring_msg);
        }


        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MobclickAgent.onEvent(App.getContext(), "v6_1_3_BW_ss");
                openActivity(SearchQuestionActivity.class);
            }
        });

        viewPager = (ViewPager) view.findViewById(R.id.id_stickynavlayout_viewpager);
        tabs = (PagerSlidingTabStrip) view.findViewById(R.id.id_stickynavlayout_indicator);

        planerAdapter = new PlanerRecycleAdapter(planerList, getActivity());
//        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
//        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 3);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setAdapter(planerAdapter);

        setQuestionTagAdapter();
        viewPager.setAdapter(pagerAdapter);
        tabs.setViewPager(viewPager);

        getData();
    }

    private void getData() {
        getHotWord();

        getPlanerData();

        getQuestionTagData();
    }

    private void setQuestionTagAdapter() {


        pagerAdapter = new FragmentStatePagerAdapter(getChildFragmentManager()) {

            @Override
            public int getCount() {
                return sortlist.size();
            }

            @Override
            public Fragment getItem(int arg0) {
                return list.get(arg0);
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return sortlist.get(position).name;
            }

            @Override
            public int getItemPosition(Object object) {
                return POSITION_NONE;
            }
        };
    }

    private void getQuestionTagData() {
        // TODO: 2017/11/6 修改测试接口
        final NetUtil sortNet = new NetUtil(getActivity(), JsonApi.QUESTION_TAGS);

        sortNet.postRequest("加载中", new NetUtil.RequestStringListener() {
            @Override
            public void onComplete(String result) {

                initData(result);
            }

        });
    }

    private void getPlanerData() {
        NetUtil planerNet = new NetUtil(getActivity(), JsonApi.PLANNER_LISTS);
        planerNet.postRequest("", new NetUtil.DataListener() {
            @Override
            public void onComplete(String data) {
                List<PlanerBean_> list = GsonUtil.fromJsonList(JSONUtil.getData("list", data), PlanerBean_.class);
                planerList.clear();
                planerList.addAll(list);
                planerAdapter.notifyDataSetChanged();

            }
        });
    }


    private void initData(String result) {
        SortBean sortItem = new Gson().fromJson(result, SortBean.class);
        sortlist.clear();
        List<SortItem> netList = sortItem.data.list;
        sortlist.addAll(netList);

        list.clear();
        for (int i = 0; i < sortlist.size(); i++) {
            list.add(QuestionListFragment.newInstance(sortlist.get(i).id));
        }
        setTabsValue();
        pagerAdapter.notifyDataSetChanged();
    }


    private void setTabsValue() {
        DisplayMetrics dm = getResources().getDisplayMetrics();
        // 设置Tab底部选中的指示器Indicator的高度
        tabs.setIndicatorHeight(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4f, dm));
        // 设置Tab底部选中的指示器 Indicator的颜色
        tabs.setIndicatorColorResource(R.color.green_lab);
        //设置指示器Indicatorin是否跟文本一样宽，默认false
        tabs.setIndicatorinFollowerTv(false);
        //设置小红点提示，item从0开始计算，true为显示，false为隐藏
        //        tabs.setMsgToast(2, true);
        //设置红点滑动到当前页面自动消失,默认为true
        tabs.setMsgToastPager(true);
        //设置Tab标题文字的颜色
        tabs.setTextColor(getResources().getColor(R.color.black));
        // 设置Tab标题文字的大小
        tabs.setTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 14, dm));
        // 设置选中的Tab文字的颜色
        tabs.setSelectedTextColorResource(R.color.green_lab);
        //设置Tab底部分割线的高度
        tabs.setUnderlineHeight(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 0.5f, dm));
        //设置Tab底部分割线的颜色
        tabs.setUnderlineColorResource(R.color.line_eb);
        // 设置点击某个Tab时的背景色,设置为0时取消背景色tabs.setTabBackground(0);
        //        tabs.setTabBackground(R.drawable.bg_tab);
        tabs.setTabBackground(0);
        // 设置Tab是自动填充满屏幕的
        tabs.setShouldExpand(true);

    }

    public void getHotWord() {
        NetUtil netUtil = new NetUtil(getActivity(), JsonApi.THINK_WORD);
        netUtil.setParams("inputPos", "qa");
        netUtil.postRequest("", new NetUtil.DataListener() {
            @Override
            public void onComplete(String data) {
                String key = JSONUtil.getData("keyword", data);
                search.setHint(key);
            }
        });
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            if (list.size() == 0 || sortlist.size() == 0) {
                getData();
            }
            //  ((QuestionListFragment) list.get(0)).autoRefresh();
        }
    }
}
